"use strict";

var setTimeNodes = function () {

    var isAddTimeNodes = false;

    var eventHandler = function () {
        $(document).off('changed.bs.select', '.first-timeDate');
        $(document).on('changed.bs.select', '.first-timeDate', dateChangeContent);

        $(document).off('click', '.set-time-nodes .date-add');
        $(document).on('click', '.set-time-nodes .date-add', addTimeNodes);

        $(document).off('click', '.set-time-nodes .date-remove');
        $(document).on('click', '.set-time-nodes .date-remove', removeTaskCreationTime);
    };


    /**
     * 发起任务管理年月日选择
     * @param e
     * @param clickedIndex
     */
    var dateChangeContent = function (e, clickedIndex) {
        debugger;
        if (!clickedIndex) {
            return;
        }
        $(".set-time-nodes .next-body").empty();
        initTimeNode({firstTimeDate: clickedIndex});
    };

    /**
     * 装填时间节点对应的html元素
     * @param options 传入的data数据。
     */
    var initTimeNode = function (options) {
        var type = $('.set-time-nodes').data('type');
        var contexts = {};
        var _options = {
            interval: '',
            firstTimeDate: 1,
            contentDate: [
                {
                    hour_val: '',
                    min_val: '',
                    hour_val_2: '',
                    min_val_2: '',
                    allowTimeLength: '',
                    create_hour: '',
                    week: '1',
                    day: '',
                    lastTimeDate: 1
                }
            ]
        };
        if (options) {
            _options = $.extend(true, _options, options);
        }

        var firstTimeDate = parseInt(_options.firstTimeDate);
        contexts.timeNodesData = _options.contentDate;
        var source;
        if (type === 'add' || type === 'edit') {
            //设置时间节点的下发间隔值。如：每 2 天 00 时创建
            $('.first-timeDate-box input[name="interval"]').val(_options.interval || '');
            $('select.first-timeDate').children().eq(firstTimeDate).prop('selected', true);
            $('.first-timeDate').selectpicker('refresh');

            if (firstTimeDate === 1) {
                //选择小时时的模板。
                source = $(".hour-time-nodes-template").html();
            } else if (firstTimeDate === 2) {
                //选择天时的模板。
                source = $(".days-time-nodes-template").html();
            } else if (firstTimeDate === 3) {
                //选择周时的模板。
                source = $(".week-time-nodes-template").html();
            } else if (firstTimeDate === 4) {
                //选择月时的模板。
                source = $(".month-time-nodes-template").html();
            }
        } else {
            contexts.timeNodesData.forEach(function (item) {
                if (parseInt(item.lastTimeDate) === 1) {
                    item.last_timeDate_label = '小时';
                }
                if (parseInt(item.lastTimeDate) === 2) {
                    item.last_timeDate_label = '日';
                }

                if (parseInt(item.week) === 1) {
                    item.week_label = '星期一'
                }
                if (parseInt(item.week) === 2) {
                    item.week_label = '星期二'
                }
                if (parseInt(item.week) === 3) {
                    item.week_label = '星期三'
                }
                if (parseInt(item.week) === 4) {
                    item.week_label = '星期四'
                }
                if (parseInt(item.week) === 5) {
                    item.week_label = '星期五'
                }
                if (parseInt(item.week) === 6) {
                    item.week_label = '星期六'
                }
                if (parseInt(item.week) === 7) {
                    item.week_label = '星期天'
                }
            });

            var firsttimeDateLabel = '';
            if (firstTimeDate === 1) {
                //选择小时时的模板。
                source = $(".view-hour-time-nodes-template").html();
                firsttimeDateLabel = '时';
            } else if (firstTimeDate === 2) {
                //选择天时的模板。
                source = $(".view-days-time-nodes-template").html();
                firsttimeDateLabel = '天';
            } else if (firstTimeDate === 3) {
                //选择周时的模板。
                source = $(".view-week-time-nodes-template").html();
                firsttimeDateLabel = '周';
            } else if (firstTimeDate === 4) {
                //选择月时的模板。
                source = $(".view-month-time-nodes-template").html();
                firsttimeDateLabel = '月';
            }

            $('.first-timeDate-box').text(_options.interval + firsttimeDateLabel);
        }

        appendHtml(contexts, source);

        $('.next-body .k_selectpicker').selectpicker();

        if (!isAddTimeNodes) {
            contexts.timeNodesData.forEach(function (item, index) {
                var nextItem = $('.next-body .next-item:eq(' + index + ')');
                nextItem.find('.last-timeDate').selectpicker('val', item.lastTimeDate);
                nextItem.find('.create-hour').selectpicker('val', item.create_hour);
            });
        }

        removeTaskCreationTime();
    };

    /**
     * 添加设置时间节点组件的html元素。
     * @param contexts
     * @param source
     */
    var appendHtml = function (contexts, source) {
        if (!source) {
            return;
        }
        var template = Handlebars.compile(source);
        $(".set-time-nodes .next-body").append(template(contexts));
    };

    //初始化任务节点前的数量以及判定删除按钮
    var initSortNum = function () {
        var dateSelectionBox = $('.set-time-nodes .next-body .next-item');
        if (dateSelectionBox.length > 1) {
            $(dateSelectionBox).find('.date-remove').prop('disabled', false);
        }
        dateSelectionBox.each(function (index, item) {
            $(item).find('.sort-num').html(index + 1);
            $(item).find('.content-1').attr('name', 'interval' + (index + 1));
            $(item).find('.content-2').attr('name', 'taskAllowTimeLength' + (index + 1));
        })
    };

    //删除任务时间节点
    var removeTaskCreationTime = function () {
        var dateSelectionBox = $('.set-time-nodes .next-body .next-item');
        $(this).parents('.next-item').remove();
        if (dateSelectionBox.length <= 2) {
            $(dateSelectionBox).find('.date-remove').prop('disabled', true);
        }

        initSortNum();
    };

    //新增任务时间节点
    var addTimeNodes = function () {
        isAddTimeNodes = true;
        var num = $('.first-timeDate-box .first-timeDate').selectpicker('val');
        initTimeNode({firstTimeDate: num});
        initSortNum();
    };

    return {
        // Init demos
        init: function () {
            eventHandler();
            $('.k_selectpicker').selectpicker();
        },
        initTimeNode: initTimeNode
    };
}();

jQuery(document).ready(function () {
    setTimeNodes.init();
});